﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Linq.Expressions;
using System.Web.DynamicData;
using System.Web.UI;
using System.Web.UI.WebControls;
using MineSolutions.ExtendedQueryableFilter.Library;

namespace MineSolutions.ExtendedQueryableFilter.WebDomainApplication
{
    public partial class StringFilter : System.Web.DynamicData.QueryableFilterUserControl
    {
        public override IQueryable GetQueryable(IQueryable source)
        {
            String selectedValue = TextBox1.Text;
            if (!String.IsNullOrWhiteSpace(selectedValue))
            {
                switch (this.GetFilterType())
                {
                    case StringFilterType.Contains:
                        return FilterQueryable.ApplyContainsFilter<String>(source, Column.Name, selectedValue);
                    case StringFilterType.StartWith:
                        return FilterQueryable.ApplyStartWithFilter<String>(source, Column.Name, selectedValue);
                    case StringFilterType.EndWith:
                        return FilterQueryable.ApplyEndsWithFilter<String>(source, Column.Name, selectedValue);
                    case StringFilterType.Equals:
                    default:
                        return FilterQueryable.ApplyEqualityFilter<String>(source, Column.Name, selectedValue);
                }
            }

            return source;
        }

        protected void TextBox1_TextChanged(object sender, EventArgs e)
        {
            OnFilterChanged();
        }

        protected void FilterRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            OnFilterChanged();
        }

        private StringFilterType GetFilterType()
        {
            if (this.EqualsRadioButton.Checked)
                return StringFilterType.Equals;
            else if (this.ContainsRadioButton.Checked)
                return StringFilterType.Contains;
            else if (this.StartWithRadioButton.Checked)
                return StringFilterType.StartWith;
            else if (this.EndWithRadioButton.Checked)
                return StringFilterType.EndWith;

            return StringFilterType.Equals;
        }
    }
}